//
//  CZStatusPictureView.swift
//  WHWeiBo01
//
//  Created by whitcast on 16/7/15.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//  重用标记
private let PictureViewCellIdentifier = "PictureViewCellIdentifier"
//  每项的间距
private let itemMargin: CGFloat = 5
//  每项的宽度
private let itemWidth: CGFloat = (ScreenWidth - 2 * HomeCellMargin - 2 * itemMargin) / 3
//  配图视图
class CZStatusPictureView: UICollectionView {
    //  配图数据源
    var picUrls: [CZStatusPictureInfo]? {
        didSet {
            messageLabel.text = "\(picUrls?.count ?? 0)"
            let size = calcPictureViewSize(picUrls?.count ?? 0)
            //  设置配图指定大小
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            
            
            //  重新加载数据
            self.reloadData()
        }
    }
    
    // MARK:    -懒加载控件
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.yellowColor(), fontSize: 30)
        return label
    }()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        //  设置大小
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //  水平间距
        flowLayout.minimumInteritemSpacing = itemMargin
        //  垂直间距
        flowLayout.minimumLineSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //  注册cell
        registerClass(CZStatusPictureViewCell.self, forCellWithReuseIdentifier: PictureViewCellIdentifier)
        //  设置数据源代理
        dataSource = self
        
        addSubview(messageLabel)
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
    }
    
    
    
    /**
     根据图片个数计算配图大小
     
     - parameter count: 配图个数
     
     - returns: 配图大小
     */
    private func calcPictureViewSize(count: Int) -> CGSize {
    
        
        
        //  列数
        let cols = count > 3 ? 3 : count
        
        //  行数
        let rows = (count - 1) / 3 + 1
        //  计算当前配图的一个宽度
        let width = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
        //  计算当前配图的一个高度
        let height = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        
        let size = CGSize(width: width, height: height)
        
        return size
        
    
    }
    


}


// MARK: -- UICollectionViewDataSource

extension CZStatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureViewCellIdentifier, forIndexPath: indexPath) as! CZStatusPictureViewCell
        //  设置模型
        cell.picInfo = picUrls![indexPath.item]
        
        return cell
    }
    

}

//  自定义配图cell
class CZStatusPictureViewCell: UICollectionViewCell {
    //  设置模型
    var picInfo: CZStatusPictureInfo? {
        didSet {
            if let imageUrl = picInfo?.thumbnail_pic {
                imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                
                //  判断我们的图片地址是否以.gif结尾
//                if imageUrl.hasSuffix(".gif") {
//                    gifImageView.hidden = false
//                } else {
//                    gifImageView.hidden = true
//                }
                //  可以抽取成一句代码
                gifImageView.hidden = !imageUrl.hasSuffix(".gif")
                
                
            }
        }
    }
    
    // MARK: -懒加载控件
    //  配图的图片
    private lazy var imageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        //  保证原始图片比例显示, 图片会超出
        view.contentMode = UIViewContentMode.ScaleAspectFill
        //  剪切超出多余部分的图片
        view.clipsToBounds = true
        return view
    }()
    //  gif图片
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
    }
    
    
    
}





















